package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 根据 jobNum 查询用户
    @GetMapping("/jobNum/{jobNum}")
    public ResponseEntity<User> getUserByJobNum(@PathVariable Integer jobNum) {
        User user = userService.findByJobNum(jobNum);

        if (user.getJobNum().describeConstable().isPresent()) {
            return ResponseEntity.ok(user);
        }

        return ResponseEntity.notFound().build();
    }

    // 更新用户字段
    @PutMapping("/update/{jobNum}")
    public boolean updateUser(
            @PathVariable Integer jobNum,
            @RequestParam(required = false) String uname,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String address,
            @RequestParam(required = false) String profession) {
        if (userService.updateUser(jobNum, uname, phone, address, profession)) {
            return true;
        }
        return false;
    }
}

